//
//  PxReaderView.h
//  Express
//
//  Created by 泰然财富 on 15/7/28.
//  Copyright (c) 2015年 泰然财富. All rights reserved.
//

#import "PxSMSModelViewController.h"
#import "LLSmallUtil.h"
#import "PxGlobal.h"
#import "PxWidgetCreater.h"
#import "PxLabel.h"

@implementation PxSMSModelViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
}

- (instancetype)initWithDataSource:(NSArray *)array
{
    self = [super initWithNibName:nil bundle:nil];
    if (self)
    {
        self.dataSource = array;
        UIWindow *keyWin = [UIApplication sharedApplication].keyWindow;
        CGFloat left = [PxGlobal getCustomerWidth:20];
        CGFloat top = [PxGlobal getCustomerHeight:110];
        CGFloat width = keyWin.width - 2*left;
        CGFloat height = [PxGlobal getCustomerHeight:207];//keyWin.height - 2*top - 64;
        self.mainView = [[UIView alloc] initWithFrame:CGRectMake(left, top + 64, width, height)];
        self.mainView.layer.cornerRadius = 3;
        [self.mainView.layer setMasksToBounds:YES];
        self.mainView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_mainView];
        
        PxLabel *headerView = [[PxLabel alloc] initWithFrame:CGRectMake(0, 0, _mainView.width, [PxGlobal getCustomerHeight:32]) andInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        headerView.text = @"请选择快递公司";
        headerView.font = [UIFont systemFontOfSize:12];
        headerView.textColor = [UIColor colorWithHTMLStr:@"707070"];
        headerView.backgroundColor = [UIColor colorWithHTMLStr:@"eaeaea"];
        [self.mainView addSubview:headerView];
        
        left = [PxGlobal getCustomerWidth:30];
        width = self.mainView.width - 2*left;
        self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(left, headerView.bottom + 15, width, [PxGlobal getCustomerHeight:90])];
        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
        self.pickerView.showsSelectionIndicator = YES;
        [self.mainView addSubview:_pickerView];
        
        UIButton *button = [PxWidgetCreater customerButton];
        [button setTitleColor:[UIColor colorWithHTMLStr:@"ffffff"] forState:UIControlStateNormal];
        [button setTitle:@"确认"];
        left = [PxGlobal getCustomerWidth:35];
        width = self.mainView.width - 2*left;
        button.frame = CGRectMake(left, self.pickerView.bottom + 15, width, [PxGlobal getCustomerHeight:35]);
        button.layer.cornerRadius = 3;
        [button setBackgroundColor:[UIColor colorWithHTMLStr:@"ff4e08"]];
        [self.mainView addSubview:button];
        [button setSelector:@selector(selected:) withTarget:self];

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletapPressGesture:)];
        [self.view addGestureRecognizer:tapGesture];
    }
    return self;
}

-(void)setData:(int) index
{
    [self.pickerView selectRow:index inComponent:0 animated:YES];
}

#pragma mark - UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSource.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [self.dataSource objectAtIndex:row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 30;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *myView = nil;
    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
    myView.textAlignment = NSTextAlignmentCenter;
    myView.text = [self.dataSource objectAtIndex:row];
    myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小
    myView.backgroundColor = [UIColor clearColor];
    
    return myView;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
    self.selectedRow = row;
}

-(void)selected:(UIButton *)sender
{
    if (self.delegate)
        [self.delegate buttonClick:self.selectedRow];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)handletapPressGesture:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self.view];
    if (!CGRectContainsPoint(self.mainView.frame, point)) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

@end